package com.js.lib_common.mvp.presenter;


import com.js.lib_common.base.BasePresenter;
import com.js.lib_common.http.ApiCallback;
import com.js.lib_common.mvp.model.CommonBean;
import com.js.lib_common.mvp.model.response.LoginResponse;
import com.js.lib_common.mvp.view.LoginView;
import com.js.lib_common.utils.MyMD5Util;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Predicate;

/**
 * Created by wz on 2021/5/17.
 */
public class LoginPresenter extends BasePresenter<LoginView> {
    public LoginPresenter(LoginView view) {
        attachView(view);
    }

    public void login(String name, String pwd) {
        mvpView.showLoading();
        addSubscription(apiStores.login(name, MyMD5Util.md5(pwd))
                        .filter(new Predicate<CommonBean<LoginResponse>>() {
                            @Override
                            public boolean test(CommonBean<LoginResponse> commonBean) throws Exception {
                                if (commonBean.isSuccess()) {
                                    return true;
                                } else {
                                    mvpView.onFailed(String.format("%s", commonBean.getMsg()));
                                    return false;
                                }
                            }
                        }).delay(800, TimeUnit.MILLISECONDS),
                new ApiCallback<CommonBean<LoginResponse>>() {
                    @Override
                    public void onSuccess(CommonBean<LoginResponse> response) {
                        mvpView.loginSuccess(response.getObj());
                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.loginFail(msg);
                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


}
